package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StatusCode;
import cn.tedu._04weibo.mapper.CommentMapper;
import cn.tedu._04weibo.pojo.dto.CommentAddParam;
import cn.tedu._04weibo.pojo.entity.Comment;
import cn.tedu._04weibo.pojo.entity.Weibo;
import cn.tedu._04weibo.pojo.vo.CommentVO;
import cn.tedu._04weibo.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("v1/comment/")
@Tag(name = "评论管理模块")
public class CommentController {
    @Autowired
    CommentMapper commentMapper;

    @PostMapping("insert")
    @Operation(summary = "01-插入评论")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody CommentAddParam commentAddParam, HttpSession session) {
        log.debug("开始处理【插入评论】的请求"+commentAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }else {
            Comment comment = new Comment();
            BeanUtils.copyProperties(commentAddParam, comment);
            comment.setUserId(userVO.getId());
            comment.setCreateTime(new Date());
            int rows = commentMapper.insert(comment);
            if (rows != 1){
                return new JsonResult(StatusCode.OPERATION_FAILED);
            }else {
                return new JsonResult(StatusCode.OPERATION_SUCCESS);
            }

        }
    }
    @GetMapping("selectByWeiboId")
    @Operation(summary = "02-查询指定微博的评论列表")
    @ApiOperationSupport(order = 20)
    public JsonResult selectByWeiboId(Integer weiboId) {
        List<CommentVO> list = commentMapper.selectByWeiboId(weiboId);
        return JsonResult.ok(list);
    }
}
